#include <gtest/gtest.h>
#include "cloud_model_audio.h"
#include "base_log.h"
#include <fstream>
#include <thread>
#include <stdio.h>
#include <thread>
#include <chrono>
#include <gtest/gtest.h>

#include "nlohmann/json.hpp"
#include "base_log.h"
#include "base_init.h"
#include "common_init.h"

namespace El {
namespace Cloud {

class CloudModelAudioTest : public ::testing::Test {
protected:
    static void SetUpTestSuite()
    {
        // 初始化基础库
        nlohmann::json config;
        config["log_file"] = "app.log";
        El::Base::base_init(config);

        // 初始化配置
        config.clear();
        config["general"] = "app_config";
        config["default"] = "default_config";
        El::Common::common_init(config);

        CloudModelAudio::GetInstance().Start();
    }

    static void TearDownTestSuite()
    {
        CloudModelAudio::GetInstance().Stop();
    }
};

TEST_F(CloudModelAudioTest, NewConversationTest)
{
    int32_t conversation_id = CloudModelAudio::GetInstance().NewConversation();
    EXPECT_GT(conversation_id, 1);
}

TEST_F(CloudModelAudioTest, DeleteConversationTest)
{
    int32_t conversation_id = CloudModelAudio::GetInstance().NewConversation();
    EXPECT_TRUE(CloudModelAudio::GetInstance().DeleteConversation(conversation_id));
    EXPECT_FALSE(CloudModelAudio::GetInstance().DeleteConversation(conversation_id));
}

TEST_F(CloudModelAudioTest, GenerateTextTest)
{
    int32_t conversation_id = CloudModelAudio::GetInstance().NewConversation();
    std::string prompt = "Hello, how are you?";
    std::string response = CloudModelAudio::GetInstance().GenerateText(conversation_id, prompt);
    EXPECT_FALSE(response.empty());
}

TEST_F(CloudModelAudioTest, ProcessAudioTest)
{
    // 创建一个临时的音频文件用于测试
    std::string temp_audio_file = "temp_audio.wav";
    std::ofstream audio_file(temp_audio_file, std::ios::binary);
    // 写入一些假数据
    for (int i = 0; i < 1000; ++i) {
        audio_file.put(static_cast<char>(i % 256));
    }
    audio_file.close();

    int32_t conversation_id = CloudModelAudio::GetInstance().NewConversation();
    std::string prompt = "Transcribe this audio";
    std::string response = CloudModelAudio::GetInstance().ProcessAudio(conversation_id, temp_audio_file, prompt);
    EXPECT_FALSE(response.empty());

    // 删除临时文件
    std::remove(temp_audio_file.c_str());
}

TEST_F(CloudModelAudioTest, MaxConversationsTest)
{
    std::vector<int32_t> conversation_ids;
    for (int i = 0; i < 6; ++i) {
        conversation_ids.push_back(CloudModelAudio::GetInstance().NewConversation());
    }
    EXPECT_EQ(conversation_ids.size(), 6);
    EXPECT_EQ(CloudModelAudio::GetInstance().NewConversation(), conversation_ids[1]);
}

} // namespace Cloud
} // namespace El